window.onload = function () {
    async function fn() {
        let types;
        let movies;
        // 发送Ajax请求
        await $.ajax({
            url: 'https://www.fastmock.site/mock/bb4157f45a0b5ffdcb3f6d984517a6c0/woniuMovie/getAllTypes',
            success: (data) => {
                types = data.types;
                typeRander(types);
            }
        });
        await $.ajax({
            url: 'https://www.fastmock.site/mock/bb4157f45a0b5ffdcb3f6d984517a6c0/woniuMovie/getAllMovies',
            success: (data) => {
                movies = data.movies;
                movieRander(movies);
            }
        });




        // 筛选列表点击事件
        $("#movietype ul").on('click', 'li', function () {
            $(this).addClass("active").siblings().removeClass("active");
            if ($(this).text() == "全部") {
                movieRander(movies);
            } else {
                let type = types.filter(type => type.name == $(this).text());
                let newMovies = movies.filter(movie => {
                    let movieTypes = movie.movieType;
                    let newMovieType = movieTypes.some(movieType => movieType == type[0].id);
                    return newMovieType;
                });
                movieRander(newMovies);
            }
        });
        $("#movieregion ul").on('click', 'li', function () {
            $(this).addClass("active").siblings().removeClass("active");
            if ($(this).text() == "全部") {
                movieRander(movies);
            } else {
                let newMovies = [];
                movies.forEach(movie => {
                    let text = $(this).text();
                    let region = movie.region;
                    if (region.indexOf(text) != -1) {
                        newMovies.push(movie);
                    }
                });
                movieRander(newMovies);
            }
        });
        $("#movieyears ul").on('click', 'li', function () {
            $(this).addClass("active").siblings().removeClass("active");
            if ($(this).text() == "全部") {
                movieRander(movies);
            } else {
                let newMovies = movies.filter(movie => movie.release.substr(0, 4) == $(this).text());
                movieRander(newMovies);
            }
        });

        // 排序
        $("#hot_sort").change(function () {
            movieRander(movies)
        });
        $("#time_sort").change(function () {
            let years = movies.map(movie => movie.release.substr(0, 4));
            let newMovies = [];
            years.sort((a, b) => b - a);
            years.forEach(year => {
                movies.forEach(movie => {
                    if (year == movie.release.substr(0, 4) && newMovies.indexOf(movie) == -1) {
                        newMovies.push(movie);
                    }
                });
            });
            movieRander(newMovies);
        });
        $("#eval_sort").change(function () {
            let scores = movies.map(movie => movie.score);
            let newMovies = [];
            scores.sort((a, b) => b - a);
            scores.forEach(score => {
                movies.forEach(movie => {
                    if (score == movie.score) {
                        newMovies.push(movie);
                    }
                });
            });
            movieRander(newMovies);
        });

        // 分类渲染
        function typeRander(types) {
            // 分类列表
            $("#movieregion ul").append($(
                `<li>大陆</li>
            <li>美国</li>
            <li>韩国</li>
            <li>日本</li>
            <li>中国香港</li>
            <li>中国台湾</li>
            <li>泰国</li>
            <li>印度</li>
            <li>法国</li>
            <li>英国</li>
            <li>俄罗斯</li>
            <li>意大利</li>
            <li>西班牙</li>
            <li>德国</li>
            <li>波兰</li>
            <li>澳大利亚</li>
            <li>伊朗</li>
            <li>其他</li>`
            ));
            $("#movieyears ul").append($(
                `<li>2021</li>
            <li>2020</li>
            <li>2019</li>
            <li>2018</li>
            <li>2017</li>
            <li>2016</li>
            <li>2015</li>
            <li>2014</li>
            <li>2013</li>
            <li>2012</li>
            <li>2011</li>
            <li>2000-2010</li>
            <li>90年代</li>
            <li>80年代</li>
            <li>70年代</li>
            <li>更早</li>`
            ));
            types.forEach(type => {
                $("#movietype ul").append($(`<li>${type.name}</li>`));
            });
        }

        // 更新电影列表
        function movieRander(movies) {
            $(".films").children().remove();
            // 影院列表
            movies.forEach(movie => {
                const ids = movie.movieType;
                let newType = '';
                ids.forEach(ID => {
                    types.forEach(type => {
                        if (ID == type.id) {
                            newType += `${type.name}/`;
                        }
                    });
                });
                newType = newType.substring(0, newType.length - 1);
                $("#movie-select .films").append($(
                    `<a href="details.html?id=${movie.id}" class="item">
                    <div class="des">
                        <img src="${movie.imgSrc}" alt="">
                        <p>${movie.title}</p>
                        <div class="introduce">
                            <p>${movie.title}<i>${movie.score}</i></p>
                            <span>类型：<em>${newType}</em></span>
                            <span>主演：<em>${movie.actors}</em></span>
                            <span>上映时间：<em>${movie.release}</em></span>
                        </div>
                    </div>
                    <i>${movie.score}</i>
                </a>`
                ));
            });
        }


    }
    fn();

};